package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;


import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }



    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findByPage(queryPageBean);
    }

    @PreAuthorize("hasAuthority('USER_ADD')")//添加用户权限
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user, Integer[] roleIds) {
        //判断用户名是否存在
        String username = user.getUsername();
        User result = userService.findByUsername(username);
        if (result != null) {
            return new Result(false, "添加失败,用户名已经存在!");
        }
        try {
            userService.addUser(user , roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        try {
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<User> userList = userService.findAll();
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户失败");
        }

    }


    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
             User user = userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"网络不佳,请刷新后重试");
        }
    }


    //标记用户信息
    @PreAuthorize("hasAuthority('USER_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

}
